package base.file.api;

import java.io.File;
import java.io.IOException;

public class DirectoryOperate {
    public static void main(String[] args) {

        createDir();

        listSubDir();
    }

    // 创建目录
    public static void createDir() {
        // 创建一级目录
        File dir = new File("C:\\Temp\\test\\createDir");
        boolean flag = dir.mkdir();
        System.out.println(flag);


        // 创建多级
        File dirs = new File("C:\\Temp\\test\\createDir2\\ccc");

        boolean flag2 = dirs.mkdirs();
        System.out.println(flag2);

        File createFile = new File("C:\\Temp\\test\\createFile.txt");
        try {
            boolean fiag3 = createFile.createNewFile();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

    }

    // 列出下级
    public static void listSubDir() {
        File dir = new File("C:\\Temp\\test");
        // 下级名称
        String[] subNames = dir.list();
        for (String s : subNames) {
            System.out.println(s);
        }

        // 下级文件
        File[]  files =  dir.listFiles();
        for (File f : files) {
            System.out.println(f.getAbsolutePath());
        }

        // 列出所有盘符
        File[] roots = File.listRoots();
        for (File f : roots) {
            System.out.println(f.getAbsolutePath());
        }
    }
}
